package com.eight.teacher.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eight.common.pojo.EduTeacher;
import com.eight.common.pojo.TbCheckTeacher;
import com.eight.teacher.dao.CheckTeacherDao;
import com.eight.teacher.dao.TbMemberDao;
import com.eight.teacher.service.CheckTeacherService;
import com.eight.teacher.vo.EduTeacherVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * @program: qy8-education
 * @description: CheckTeacherServiceImpl
 * @author: 韩宗耀
 * @create: 2023-05-27 21:47
 **/
@Service
public class CheckTeacherServiceImpl implements CheckTeacherService {
    @Resource
    private CheckTeacherDao checkTeacherDao;
    @Resource
    private TbMemberDao tbMemberDao;

    @Override
    public IPage<TbCheckTeacher> findTeacherCheck(Integer pageCurrent, Integer pageSize, EduTeacherVo eduTeacherVo) {
        Page<EduTeacher> page = new Page<>(pageCurrent, pageSize);
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(eduTeacherVo.getName())) {
            wrapper.like("name",eduTeacherVo.getName());
        }
        if (StringUtils.hasText(eduTeacherVo.getMobile())) {
            wrapper.like("mobile",eduTeacherVo.getMobile());
        }
        if (StringUtils.hasText(eduTeacherVo.getStatus())) {
            wrapper.like("status",eduTeacherVo.getStatus());
        }
        wrapper.eq("check_status","2");
        return checkTeacherDao.findTeacherCheck(page,wrapper);
    }

    @Override
    public Integer changeTeacherCheck(TbCheckTeacher tbCheckTeacher) {
        int i = checkTeacherDao.updateById(tbCheckTeacher);
        /**
         * 通过ID 查找 check_status
         */
        Integer teacherCheckId = tbCheckTeacher.getTeacherCheckId();
        QueryWrapper<TbCheckTeacher> tbCheckTeacherQueryWrapper1 = new QueryWrapper<>();
        tbCheckTeacherQueryWrapper1.eq("teacher_check_id",teacherCheckId);
        TbCheckTeacher tbCheckTeacher2 = checkTeacherDao.selectOne(tbCheckTeacherQueryWrapper1);


        QueryWrapper<TbCheckTeacher> tbCheckTeacherQueryWrapper = new QueryWrapper<>();
        String teacherId = tbCheckTeacher2.getTeacherId();
        tbCheckTeacherQueryWrapper.eq("teacher_id",teacherId);
        TbCheckTeacher tbCheckTeacher1 = checkTeacherDao.selectOne(tbCheckTeacherQueryWrapper);
        String checkStatus = tbCheckTeacher1.getCheckStatus();
        if(checkStatus.equals("1")){
            tbMemberDao.updateByIdPass(teacherId);
        }else {
            tbMemberDao.updateByIdNoPass(teacherId);
        }

        return i;
    }
}
